from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json
from typing import Dict, Any

mcp = FastMCP("Deepin-Anything MCP Server")

"""
Deepin-Anything MCP Server

面向软件：deepin-anything
这是一个用于文件索引和搜索的工具，提供快速文件查找功能。

MCP tools 列表：
1. deepin_anything_service_status - 检查 deepin-anything-tool.service 的运行状态
   参数：无
   返回：包含服务状态的统一JSON结构

2. deepin_anything_service_start - 启动 deepin-anything-tool.service
   参数：无
   返回：包含启动结果的统一JSON结构

3. deepin_anything_service_stop - 停止 deepin-anything-tool.service
   参数：无
   返回：包含停止结果的统一JSON结构

4. deepin_anything_service_restart - 重启 deepin-anything-tool.service
   参数：无
   返回：包含重启结果的统一JSON结构

5. deepin_anything_service_reload - 重载 deepin-anything-tool.service
   参数：无
   返回：包含重载结果的统一JSON结构

所有工具返回的统一JSON结构：
{
  "success": bool,      # 命令是否成功执行
  "command": str,       # 执行的完整命令
  "exit_code": int,     # 命令的退出码
  "stdout": str,        # 标准输出内容
  "stderr": str         # 标准错误内容
}
"""

@mcp.tool()
def deepin_anything_service_status() -> Dict[str, Any]:
    """检查 deepin-anything-tool.service 的运行状态
    
    返回：
        Dict[str, Any]: 包含服务状态信息的统一JSON结构
    """
    command = ["systemctl", "status", "deepin-anything-tool.service"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def deepin_anything_service_start() -> Dict[str, Any]:
    """启动 deepin-anything-tool.service
    
    返回：
        Dict[str, Any]: 包含启动结果的统一JSON结构
    """
    command = ["systemctl", "start", "deepin-anything-tool.service"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def deepin_anything_service_stop() -> Dict[str, Any]:
    """停止 deepin-anything-tool.service
    
    返回：
        Dict[str, Any]: 包含停止结果的统一JSON结构
    """
    command = ["systemctl", "stop", "deepin-anything-tool.service"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def deepin_anything_service_restart() -> Dict[str, Any]:
    """重启 deepin-anything-tool.service
    
    返回：
        Dict[str, Any]: 包含重启结果的统一JSON结构
    """
    command = ["systemctl", "restart", "deepin-anything-tool.service"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

@mcp.tool()
def deepin_anything_service_reload() -> Dict[str, Any]:
    """重载 deepin-anything-tool.service
    
    返回：
        Dict[str, Any]: 包含重载结果的统一JSON结构
    """
    command = ["systemctl", "reload", "deepin-anything-tool.service"]
    result = subprocess.run(command, capture_output=True, text=True)
    
    return {
        "success": result.returncode == 0,
        "command": " ".join(command),
        "exit_code": result.returncode,
        "stdout": result.stdout,
        "stderr": result.stderr
    }

if __name__ == "__main__":
    mcp.run()